﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BGSpaceShooterServer;

namespace BGSpaceShooter
{
    /// <summary>
    /// Klasa pomocnicza - tworzy XMLowy opis Starfightera.
    /// </summary>
    public class XMLDescriptor
    {
        private StarFighter[] starFighters;
        private XDocument document;

        public XMLDescriptor(StarFighter[] starFighters)
        {
            document = new XDocument();

            this.starFighters = starFighters;
        }

        public void CreateXML(string path, BGSpaceShooterServer.GameObjectName objectName)
        {
            int i = 0;
            foreach (StarFighter fighter in starFighters)
            {
                XElement element = new XElement("StarFighter");
                element.SetAttributeValue("Name", objectName);

                element.SetAttributeValue("ID", i++);

                element.SetAttributeValue("HP", fighter.MaxHP);

                element.SetAttributeValue("VelocityX", fighter.MaxVelocity.X);
                element.SetAttributeValue("VelocityY", fighter.MaxVelocity.Y);
                element.SetAttributeValue("VelocityZ", fighter.MaxVelocity.Z);

                element.SetAttributeValue("AccelerationX", fighter.Acceleration.X);
                element.SetAttributeValue("AccelerationY", fighter.Acceleration.X);
                element.SetAttributeValue("AccelerationZ", fighter.Acceleration.X);

                element.SetAttributeValue("Radius", fighter.BoundingSphere.Radius);

                int j = 0;

                foreach (StarFighterComponent starFighterComponent in fighter.StarFighterComponents)
                {
                    XElement sfcElement = new XElement("StarFighterComponent");

                    sfcElement.SetAttributeValue("ID", j++);

                    sfcElement.SetAttributeValue("CenterX", starFighterComponent.BoundingSphere.Center.X);
                    sfcElement.SetAttributeValue("CenterY", starFighterComponent.BoundingSphere.Center.Y);
                    sfcElement.SetAttributeValue("CenterZ", starFighterComponent.BoundingSphere.Center.Z);

                    sfcElement.SetAttributeValue("Radius", starFighterComponent.BoundingSphere.Radius);

                    sfcElement.SetAttributeValue("HP", starFighterComponent.MaxHP);

                    sfcElement.SetValue(starFighterComponent.MeshName);

                    element.Add(sfcElement);

                }
                document.Add(element);

                document.Save(path + "\\StarFighterStats.xml");
            }
        }
    }
}
